package com.example.hzly.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hzly.entity.StoryFavorite;
import com.example.hzly.mapper.StoryFavoriteMapper;
import com.example.hzly.service.StoryFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StoryFavoriteServiceImpl extends ServiceImpl<StoryFavoriteMapper, StoryFavorite> implements StoryFavoriteService {
    @Autowired
    private StoryFavoriteMapper storyFavoriteMapper;

    @Override
    public boolean addFavorite(Long userId, Long storyId) {
        if (isFavorite(userId, storyId)) return false;
        StoryFavorite favorite = new StoryFavorite();
        favorite.setUserId(userId);
        favorite.setStoryId(storyId);
        return save(favorite);
    }

    @Override
    public boolean removeFavorite(Long userId, Long storyId) {
        return lambdaUpdate().eq(StoryFavorite::getUserId, userId).eq(StoryFavorite::getStoryId, storyId).remove();
    }

    @Override
    public boolean isFavorite(Long userId, Long storyId) {
        return storyFavoriteMapper.findByUserAndStory(userId, storyId) != null;
    }
} 